package com.cn.boot.spring.controller;

import com.cn.boot.spring.entity.DepartmentEntity;
import com.cn.boot.spring.service.IDepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Api(tags = "部门管理")
@RequestMapping("/admin/department")
public class DepartmentController {
    @Autowired
    private IDepartmentService iDepartmentService;
    @ApiOperation("部门列表")
    @GetMapping("/findAll")
    public List<DepartmentEntity> findAll(){
        return iDepartmentService.findAll();
    }
    @ApiOperation("根据id查找部门")
    @GetMapping("/findById1")
    public DepartmentEntity findById1(@RequestParam Integer id) {
        return iDepartmentService.findById(id);
    }
    @ApiOperation("根据id查找部门")
    @GetMapping("/findById2/{id}")
    public DepartmentEntity findById2(@PathVariable Integer id) {
        return iDepartmentService.findById(id);
    }


    /**
     * 添加一条数据
     * Post请求要设置请求方法，参数注解要用@RequestBody
     */
    @ApiOperation("添加一个部门")
    @PostMapping("/add")
    public Integer add(@RequestBody DepartmentEntity departmentEntity) {
        return iDepartmentService.add(departmentEntity);
    }

    /**
     * 批量添加
     * 参数传入list
     */
    @ApiOperation("添加多个部门")
    @PostMapping("/adds")
    public Integer adds(@RequestBody List<DepartmentEntity> list) {
        iDepartmentService.adds(list);
//        System.out.println(users);
        //添加几条就返回几条
        return list.size();
    }

    /**
     * 批量删除
     */
    @ApiOperation("批量删除")
    @DeleteMapping("/del")
    public Integer del(@RequestBody List<Integer> ids) {
        iDepartmentService.del(ids);
        //删除几条就返回几条
        return ids.size();
    }
    /**
     * 更新
     */
    @ApiOperation("数据更新")
    @PostMapping("/update")
    public Integer update(@RequestBody DepartmentEntity departmentEntity){
        return iDepartmentService.update(departmentEntity);
    }
}
